define(['./PageView', "jquery", '../libs/canlendar', '../libs/AjaxUtil', '../libs/CommonTools', '../libs/GetAction', '../libs/util'], function (PageView, $, Canlendar, AjaxUtil, CommonTools, GetAction, util) {

    return PageView.extend({
        events:{
            'click .delete-operation': 'delKeyResult',
            'click .edit-operation': 'editKeyResult'
        },
        initialize: function () {
            require('../../less/KeyResultDetail.less');
        },
        render: function () {
            PageView.prototype.render.apply(this, arguments);
            var tpl = require('../templates/KeyResultDetail.html');
            this.$main = $('<div class="main"></div>');
            this.$wrap = $('<div class="content"></div>');
            this.$wrap.append(tpl);
            this.$main.append(this.$wrap);
            this.$el.append(this.$main);
            return this;
        },
        onRender: function(){
            var keyResults = JSON.parse(sessionStorage.getItem("goalData")).keyResult;
            var keyId = this.getUrlParam("key_id");
            var keyInfo;
            for(var i=0 ; i<keyResults.length ; i++){
                if(keyResults[i].id == keyId){
                    keyInfo = keyResults[i];
                    break;
                }
            }
            this.setKeyInfo(keyInfo);
            // if(window.app.goalData){
            //     this.$el.find('.key-result-content').text();
            // }
        },
        onResume: function(){
            this.onRender();
        },
        setKeyInfo: function(keyInfo){
            this.$el.find(".key-result-content").text(keyInfo.description);
            this.$el.find(".progress-percent").text(keyInfo.progress+"%");
        },
        getUrlParam: function(paramName){
            //获取URL上的参数
            var url = location.href; //获取url中"?"符后的字串
            var index = url.indexOf("#");
            url = url.substr(index);
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                index = url.indexOf("?");
                var str = url.substr(index+1);
                var strs = str.split("&");
                for(var i = 0; i < strs.length; i ++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            if(paramName=="target_id"){
                return theRequest.target_id;
            }
            else if(paramName=="key_id"){
                return theRequest.key_id;
            }
            else{
                return undefined;
            }
        },
        editKeyResult: function(){
            var key_id = this.getUrlParam("key_id");
            var target_id = this.getUrlParam("target_id");
            Backbone.history.navigate('#KeyResultAdd?target_id='+target_id
                +'&key_id='+key_id,{
                trigger:true
            });
        },
        delKeyResult: function(){
            var _this = this;
            CommonTools.confirm('确认删除关键结果吗?',function(){
                var key_id = _this.getUrlParam("key_id");
                var target_id = _this.getUrlParam("target_id");
                AjaxUtil.ajaxPost({
                    path: '/client/keyResult/delete',
                    params: {id:key_id,objectiveId:target_id},
                    success: function (data) {
                        CommonTools.Toast("删除关键结果成功!",1000,function(){_this.back()});

                    },
                    error: function () {
                        CommonTools.Toast("请检查网络!");
                    }
                });
            }, function(){},"");
        },
        back: function () {
            Backbone.history.history.back();
        },
    });
});
